﻿/***********************************************************************
 * <copyright file="SqlServerAccountBalanceDao.cs" company="BUCA JSC">
 * -->    Copyright (C) statement. All right reserved
 * </copyright>
 * 
 * Created:   ThangND
 * Email:    thangnd@buca.vn
 * Website:
 * Create Date: 27 March 2014
 * Usage: 
 * 
 * RevisionHistory: 
 * Date         Author               Description 
 * 
 * ************************************************************************/

using System;
using System.Data;
using BusinessObjects.BussinessVoucher;
using DataObjects.Shared;
using DataObjects.StandartInterfaces;

namespace DataObjects.ADO.SqlServer
{
    /// <summary>
    /// SqlServerAccountBalanceDao
    /// </summary>
    public class SqlServerAccountBalanceDao : IAccountBalanceDao
    {
        /// <summary>
        /// Gets the accountBalance.
        /// </summary>
        /// <param name="accountBalanceId">The accountBalance identifier.</param>
        /// <returns></returns>
        /// <exception cref="System.NotImplementedException"></exception>
        public AccountBalance GetAccountBalance(int accountBalanceId)
        {
            const string sql = @"uspGet_AccountBalance_ByID";

            object[] parms = { "@AccountBalanceId", accountBalanceId };
            return Db.Read(sql, CommandType.StoredProcedure, Make, parms);
        }

        /// <summary>
        /// Gets the account balance by account number.
        /// </summary>
        /// <param name="accountNumber">The account number.</param>
        /// <returns></returns>
        public AccountBalance GetAccountBalanceByAccountNumber(string accountNumber)
        {
            const string sql = @"uspGet_AccountBalance_ByAccountNumber";

            object[] parms = { "@AccountNumber", accountNumber };
            return Db.Read(sql, CommandType.StoredProcedure, Make, parms);
        }

        /// <summary>
        /// Gets the exits account balance.
        /// </summary>
        /// <param name="accountBalance">The account balance.</param>
        /// <returns></returns>
        public AccountBalance GetExitsAccountBalance(AccountBalance accountBalance)
        {
            const string sql = @"uspGet_Exist_AccountBalance";

            object[] parms = { "@BalanceDate", accountBalance.BalanceDate,
                              
                               "@AccountNumber", accountBalance.AccountNumber,
                              
                               "@UnitID", accountBalance.UnitId };
            return Db.Read(sql, CommandType.StoredProcedure, Make, parms);
        }

        /// <summary>
        /// Inserts the account balance.
        /// </summary>
        /// <param name="accountBalance">The account balance.</param>
        /// <returns></returns>
        public string InsertAccountBalance(AccountBalance accountBalance)
        {
            const string sql = @"uspInsert_AccountBalance";
            return Db.Insert(sql, CommandType.StoredProcedure, Take(accountBalance));
        }

        /// <summary>
        /// Updates the account balance.
        /// </summary>
        /// <param name="accountBalance">The journal entry account.</param>
        /// <returns></returns>
        public string UpdateAccountBalance(AccountBalance accountBalance)
        {
            const string sql = @"uspUpdate_AccountBalance";
            return Db.Update(sql, CommandType.StoredProcedure, Take(accountBalance));
        }

        /// <summary>
        /// Deletes the account balance.
        /// </summary>
        /// <param name="accountBalance">The journal entry account.</param>
        /// <returns></returns>
        public string DeleteAccountBalance(AccountBalance accountBalance)
        {
            const string sql = @"uspDelete_AccountBalance";

            object[] parms = { "@AccountBalanceID", accountBalance.AccountBalanceId };
            return Db.Delete(sql, CommandType.StoredProcedure, parms);
        }
        /// <summary>
        /// Deletes the account balance.
        /// </summary>
        /// <returns></returns>
        public string DeleteAccountBalance()
        {
            const string sql = @"uspDelete_AccountBalance_SumAmountNull";
            return Db.Delete(sql, CommandType.StoredProcedure);
        }

        /// <summary>
        /// The make
        /// </summary>
        private static readonly Func<IDataReader, AccountBalance> Make = reader =>
            new AccountBalance
            {
                AccountBalanceId = reader["AccountBalanceID"].AsInt(),
                BalanceDate = reader["BalanceDate"].AsDateTime(),
              
                AccountNumber = reader["AccountNumber"].AsString(),
              
                MovementCreditAmountOC = reader["MovementCreditAmountOC"].AsDecimal(),
              
                MovementDebitAmountOC = reader["MovementDebitAmountOC"].AsDecimal(),
              
                CustomerId = reader["CustomerID"].AsString().AsIntForNull(),
                VendorId = reader["VendorID"].AsString().AsIntForNull(),

                UnitId = reader["UnitID"].AsString().AsInt()
            };

        /// <summary>
        /// Takes the specified account balance.
        /// </summary>
        /// <param name="accountBalance">The account balance.</param>
        /// <returns></returns>
        private static object[] Take(AccountBalance accountBalance)
        {
            return new object[]  
            {
                @"AccountBalanceID", accountBalance.AccountBalanceId,
                @"BalanceDate", accountBalance.BalanceDate,

                @"AccountNumber", accountBalance.AccountNumber,

                @"MovementCreditAmountOC", accountBalance.MovementCreditAmountOC,

                @"MovementDebitAmountOC", accountBalance.MovementDebitAmountOC,

                @"CustomerID", accountBalance.CustomerId,
                @"VendorID", accountBalance.VendorId,

                @"UnitID", accountBalance.UnitId
            };
        }
    }
}